const mysql = require('mysql');

const dbConfig = {
  host: 'localhost', // 数据库地址
  user: 'root', // 数据库用户名
  password: '123456', // 数据库密码
  database: 'chengfeng_inn', // 数据库名
};

async function connect() {
  try {
    const connection = mysql.createConnection(dbConfig);
    console.log('Successfully connected to the database.');
    return connection;
  } catch (error) {
    console.error('Error connecting to the database:', error);
    throw error;
  }
}

module.exports = { connect };

// connection.connect((err) => {
//   if (err) throw err;
//   console.log('Connected to the chengfeng_inn database.');
// });

// 不要忘记在操作完成后关闭连接
// connection.end((err) => {
//   if (err) throw err;
//   console.log('Connection closed.');
// });
